home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Interfaces&Libraries / Universal / Interfaces / CIncludes / Endian.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-17  |  11.5 KB  |  427 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Endian.h
  3.  
  4.      Contains:    QuickTime Interfaces
  5.  
  6.      Version:    Technology:    QuickTime 3.0
  7.                  Release:    Universal Interfaces 3.2
  8.  
  9.      Copyright:    © 1997-1998 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Bugs?:        For bug reports, consult the following page on
  12.                  the World Wide Web:
  13.  
  14.                      http://developer.apple.com/bugreporter/
  15.  
  16. */
  17. #ifndef __ENDIAN__
  18. #define __ENDIAN__
  19.  
  20. #ifndef __CONDITIONALMACROS__
  21. #include <ConditionalMacros.h>
  22. #endif
  23. #ifndef __MACTYPES__
  24. #include <MacTypes.h>
  25. #endif
  26.  
  27.  
  28.  
  29. #if PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32.  
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36.  
  37. #if PRAGMA_IMPORT
  38. #pragma import on
  39. #endif
  40.  
  41. #if PRAGMA_STRUCT_ALIGN
  42.     #pragma options align=mac68k
  43. #elif PRAGMA_STRUCT_PACKPUSH
  44.     #pragma pack(push, 2)
  45. #elif PRAGMA_STRUCT_PACK
  46.     #pragma pack(2)
  47. #endif
  48.  
  49. /*
  50.     This file provides Endian Flipping routines for dealing with converting data
  51.     between Big-Endian and Little-Endian machines.  These routines are useful
  52.     when writing code to compile for both Big and Little Endian machines and  
  53.     which must handle other endian number formats, such as reading or writing 
  54.     to a file or network packet.
  55.     
  56.     These routines are named as follows:
  57.     
  58.         Endian<U><W>_<S>to<D>
  59.  
  60.     where
  61.         <U>    is whether the integer is signed ('S') or unsigned ('U')
  62.         <W> is integer bit width: 16, 32, or 64 
  63.         <S> is the source endian format: 'B' for big, 'L' for little, or 'N' for native
  64.         <D> is the destination endian format: 'B' for big, 'L' for little, or 'N' for native
  65.     
  66.     For example, to convert a Big Endian 32-bit unsigned integer to the current native format use:
  67.         
  68.         long i = EndianU32_BtoN(data);
  69.         
  70.     This file is set up so that the function macro to nothing when the target runtime already
  71.     is the desired format (e.g. on Big Endian machines, EndianU32_BtoN() macros away).
  72.             
  73.     If long long's are not supported, you cannot get 64-bit quantities as a single value.
  74.     The macros are not defined in that case.
  75.     
  76.     
  77.     
  78.                                 <<< W A R N I N G >>>
  79.     
  80.     It is very important not to put any autoincrements inside the macros.  This 
  81.     will produce erroneous results because each time the address is accessed in the macro, 
  82.     the increment occurs.
  83.     
  84.  */
  85. EXTERN_API_C( SInt16 )
  86. EndianS16_BtoN                    (SInt16                 value);
  87.  
  88. EXTERN_API_C( SInt16 )
  89. EndianS16_NtoB                    (SInt16                 value);
  90.  
  91. EXTERN_API_C( SInt16 )
  92. EndianS16_LtoN                    (SInt16                 value);
  93.  
  94. EXTERN_API_C( SInt16 )
  95. EndianS16_NtoL                    (SInt16                 value);
  96.  
  97. EXTERN_API_C( SInt16 )
  98. EndianS16_LtoB                    (SInt16                 value);
  99.  
  100. EXTERN_API_C( SInt16 )
  101. EndianS16_BtoL                    (SInt16                 value);
  102.  
  103. EXTERN_API_C( UInt16 )
  104. EndianU16_BtoN                    (UInt16                 value);
  105.  
  106. EXTERN_API_C( UInt16 )
  107. EndianU16_NtoB                    (UInt16                 value);
  108.  
  109. EXTERN_API_C( UInt16 )
  110. EndianU16_LtoN                    (UInt16                 value);
  111.  
  112. EXTERN_API_C( UInt16 )
  113. EndianU16_NtoL                    (UInt16                 value);
  114.  
  115. EXTERN_API_C( UInt16 )
  116. EndianU16_LtoB                    (UInt16                 value);
  117.  
  118. EXTERN_API_C( UInt16 )
  119. EndianU16_BtoL                    (UInt16                 value);
  120.  
  121. EXTERN_API_C( SInt32 )
  122. EndianS32_BtoN                    (SInt32                 value);
  123.  
  124. EXTERN_API_C( SInt32 )
  125. EndianS32_NtoB                    (SInt32                 value);
  126.  
  127. EXTERN_API_C( SInt32 )
  128. EndianS32_LtoN                    (SInt32                 value);
  129.  
  130. EXTERN_API_C( SInt32 )
  131. EndianS32_NtoL                    (SInt32                 value);
  132.  
  133. EXTERN_API_C( SInt32 )
  134. EndianS32_LtoB                    (SInt32                 value);
  135.  
  136. EXTERN_API_C( SInt32 )
  137. EndianS32_BtoL                    (SInt32                 value);
  138.  
  139. EXTERN_API_C( UInt32 )
  140. EndianU32_BtoN                    (UInt32                 value);
  141.  
  142. EXTERN_API_C( UInt32 )
  143. EndianU32_NtoB                    (UInt32                 value);
  144.  
  145. EXTERN_API_C( UInt32 )
  146. EndianU32_LtoN                    (UInt32                 value);
  147.  
  148. EXTERN_API_C( UInt32 )
  149. EndianU32_NtoL                    (UInt32                 value);
  150.  
  151. EXTERN_API_C( UInt32 )
  152. EndianU32_LtoB                    (UInt32                 value);
  153.  
  154. EXTERN_API_C( UInt32 )
  155. EndianU32_BtoL                    (UInt32                 value);
  156.  
  157. EXTERN_API_C( SInt64 )
  158. EndianS64_BtoN                    (SInt64                 value);
  159.  
  160. EXTERN_API_C( SInt64 )
  161. EndianS64_NtoB                    (SInt64                 value);
  162.  
  163. EXTERN_API_C( SInt64 )
  164. EndianS64_LtoN                    (SInt64                 value);
  165.  
  166. EXTERN_API_C( SInt64 )
  167. EndianS64_NtoL                    (SInt64                 value);
  168.  
  169. EXTERN_API_C( SInt64 )
  170. EndianS64_LtoB                    (SInt64                 value);
  171.  
  172. EXTERN_API_C( SInt64 )
  173. EndianS64_BtoL                    (SInt64                 value);
  174.  
  175. EXTERN_API_C( UInt64 )
  176. EndianU64_BtoN                    (UInt64                 value);
  177.  
  178. EXTERN_API_C( UInt64 )
  179. EndianU64_NtoB                    (UInt64                 value);
  180.  
  181. EXTERN_API_C( UInt64 )
  182. EndianU64_LtoN                    (UInt64                 value);
  183.  
  184. EXTERN_API_C( UInt64 )
  185. EndianU64_NtoL                    (UInt64                 value);
  186.  
  187. EXTERN_API_C( UInt64 )
  188. EndianU64_LtoB                    (UInt64                 value);
  189.  
  190. EXTERN_API_C( UInt64 )
  191. EndianU64_BtoL                    (UInt64                 value);
  192.  
  193. /*
  194.    These types are used for structures that contain data that is
  195.    always in BigEndian format.  This extra typing prevents little
  196.    endian code from directly changing the data, thus saving much
  197.    time in the debugger.
  198. */
  199. #if TARGET_RT_LITTLE_ENDIAN
  200.  
  201. struct BigEndianLong {
  202.     long                             bigEndianValue;
  203. };
  204. typedef struct BigEndianLong            BigEndianLong;
  205.  
  206. struct BigEndianUnsignedLong {
  207.     unsigned long                     bigEndianValue;
  208. };
  209. typedef struct BigEndianUnsignedLong    BigEndianUnsignedLong;
  210.  
  211. struct BigEndianShort {
  212.     short                             bigEndianValue;
  213. };
  214. typedef struct BigEndianShort            BigEndianShort;
  215.  
  216. struct BigEndianUnsignedShort {
  217.     unsigned short                     bigEndianValue;
  218. };
  219. typedef struct BigEndianUnsignedShort    BigEndianUnsignedShort;
  220.  
  221. struct BigEndianFixed {
  222.     Fixed                             bigEndianValue;
  223. };
  224. typedef struct BigEndianFixed            BigEndianFixed;
  225.  
  226. struct BigEndianUnsignedFixed {
  227.     UnsignedFixed                     bigEndianValue;
  228. };
  229. typedef struct BigEndianUnsignedFixed    BigEndianUnsignedFixed;
  230.  
  231. struct BigEndianOSType {
  232.     OSType                             bigEndianValue;
  233. };
  234. typedef struct BigEndianOSType            BigEndianOSType;
  235. #else
  236.  
  237. typedef long                             BigEndianLong;
  238. typedef unsigned long                     BigEndianUnsignedLong;
  239. typedef short                             BigEndianShort;
  240. typedef unsigned short                     BigEndianUnsignedShort;
  241. typedef Fixed                             BigEndianFixed;
  242. typedef UnsignedFixed                     BigEndianUnsignedFixed;
  243. typedef OSType                             BigEndianOSType;
  244. #endif  /* TARGET_RT_LITTLE_ENDIAN */
  245.  
  246.  
  247. /*
  248.     Macro away no-op functions
  249. */
  250. #if TARGET_RT_BIG_ENDIAN
  251.     #define EndianS16_BtoN(value)                (value)
  252.     #define EndianS16_NtoB(value)                (value)
  253.     #define EndianU16_BtoN(value)                (value)
  254.     #define EndianU16_NtoB(value)                (value)
  255.     #define EndianS32_BtoN(value)                (value)
  256.     #define EndianS32_NtoB(value)                (value)
  257.     #define EndianU32_BtoN(value)                (value)
  258.     #define EndianU32_NtoB(value)                (value)
  259.     #define EndianS64_BtoN(value)                (value)
  260.     #define EndianS64_NtoB(value)                (value)
  261.     #define EndianU64_BtoN(value)                (value)
  262.     #define EndianU64_NtoB(value)                (value)
  263. #else
  264.     #define EndianS16_LtoN(value)                (value)
  265.     #define EndianS16_NtoL(value)                (value)
  266.     #define EndianU16_LtoN(value)                (value)
  267.     #define EndianU16_NtoL(value)                (value)
  268.     #define EndianS32_LtoN(value)                (value)
  269.     #define EndianS32_NtoL(value)                (value)
  270.     #define EndianU32_LtoN(value)                (value)
  271.     #define EndianU32_NtoL(value)                (value)
  272.     #define EndianS64_LtoN(value)                (value)
  273.     #define EndianS64_NtoL(value)                (value)
  274.     #define EndianU64_LtoN(value)                (value)
  275.     #define EndianU64_NtoL(value)                (value)
  276. #endif
  277.  
  278.  
  279.  
  280. /*
  281.     Map native to actual
  282. */
  283. #if TARGET_RT_BIG_ENDIAN
  284.     #define EndianS16_LtoN(value)                EndianS16_LtoB(value)
  285.     #define EndianS16_NtoL(value)                EndianS16_BtoL(value)
  286.     #define EndianU16_LtoN(value)                EndianU16_LtoB(value)
  287.     #define EndianU16_NtoL(value)                EndianU16_BtoL(value)
  288.     #define EndianS32_LtoN(value)                EndianS32_LtoB(value)
  289.     #define EndianS32_NtoL(value)                EndianS32_BtoL(value)
  290.     #define EndianU32_LtoN(value)                EndianU32_LtoB(value)
  291.     #define EndianU32_NtoL(value)                EndianU32_BtoL(value)
  292.     #define EndianS64_LtoN(value)                EndianS64_LtoB(value)
  293.     #define EndianS64_NtoL(value)                EndianS64_BtoL(value)
  294.     #define EndianU64_LtoN(value)                EndianU64_LtoB(value)
  295.     #define EndianU64_NtoL(value)                EndianU64_BtoL(value)
  296. #else
  297.     #define EndianS16_BtoN(value)                EndianS16_BtoL(value)
  298.     #define EndianS16_NtoB(value)                EndianS16_LtoB(value)
  299.     #define EndianU16_BtoN(value)                EndianU16_BtoL(value)
  300.     #define EndianU16_NtoB(value)                EndianU16_LtoB(value)
  301.     #define EndianS32_BtoN(value)                EndianS32_BtoL(value)
  302.     #define EndianS32_NtoB(value)                EndianS32_LtoB(value)
  303.     #define EndianU32_BtoN(value)                EndianU32_BtoL(value)
  304.     #define EndianU32_NtoB(value)                EndianU32_LtoB(value)
  305.     #define EndianS64_BtoN(value)                EndianS64_BtoL(value)
  306.     #define EndianS64_NtoB(value)                EndianS64_LtoB(value)
  307.     #define EndianU64_BtoN(value)                EndianU64_BtoL(value)
  308.     #define EndianU64_NtoB(value)                EndianU64_LtoB(value)
  309. #endif
  310.  
  311.  
  312.  
  313. /*
  314.     Implement ≈LtoB and ≈BtoL
  315. */
  316. #define EndianS16_LtoB(value)                ((SInt16)Endian16_Swap(value))
  317. #define EndianS16_BtoL(value)                ((SInt16)Endian16_Swap(value))
  318. #define EndianU16_LtoB(value)                ((UInt16)Endian16_Swap(value))
  319. #define EndianU16_BtoL(value)                ((UInt16)Endian16_Swap(value))
  320. #define EndianS32_LtoB(value)                ((SInt32)Endian32_Swap(value))
  321. #define EndianS32_BtoL(value)                ((SInt32)Endian32_Swap(value))
  322. #define EndianU32_LtoB(value)                ((UInt32)Endian32_Swap(value))
  323. #define EndianU32_BtoL(value)                ((UInt32)Endian32_Swap(value))
  324. #define EndianS64_LtoB(value)                ((SInt64)Endian64_Swap((UInt64)value))
  325. #define EndianS64_BtoL(value)                ((SInt64)Endian64_Swap((UInt64)value))
  326. #define EndianU64_LtoB(value)                ((UInt64)Endian64_Swap(value))
  327. #define EndianU64_BtoL(value)                ((UInt64)Endian64_Swap(value))
  328.  
  329.  
  330.  
  331. /*
  332.     Implement low level ≈_Swap functions.
  333.     
  334.         extern UInt16 Endian16_Swap(UInt16 value);
  335.         extern UInt32 Endian32_Swap(UInt32 value);
  336.         extern UInt64 Endian64_Swap(UInt64 value);
  337.         
  338.     Note: Depending on the processor, you might want to implement
  339.           these as function calls instead of macros.
  340.     
  341. */
  342.  
  343. #if TARGET_CPU_68K && TARGET_OS_MAC
  344.     #pragma parameter __D0 Endian16_Swap(__D0)
  345.     pascal UInt16 Endian16_Swap(UInt16 value)
  346.         = { 0xE158 };
  347.     
  348.     #pragma parameter __D0 Endian32_Swap (__D0)
  349.     pascal UInt32 Endian32_Swap(UInt32 value)
  350.         = { 0xE158, 0x4840, 0xE158 };
  351. #else
  352.     #define Endian16_Swap(value)                 \
  353.             (((((UInt16)value)<<8) & 0xFF00)   | \
  354.              ((((UInt16)value)>>8) & 0x00FF))
  355.     
  356.     #define Endian32_Swap(value)                     \
  357.             (((((UInt32)value)<<24) & 0xFF000000)  | \
  358.              ((((UInt32)value)<< 8) & 0x00FF0000)  | \
  359.              ((((UInt32)value)>> 8) & 0x0000FF00)  | \
  360.              ((((UInt32)value)>>24) & 0x000000FF))
  361. #endif
  362.  
  363.  
  364. #if TYPE_LONGLONG
  365.     #ifdef __MRC__
  366.         #define Endian64_Swap(value)                             \
  367.                 (((((UInt64)value)<<56) & 0xFF00000000000000ULL)  | \
  368.                  ((((UInt64)value)<<40) & 0x00FF000000000000ULL)  | \
  369.                  ((((UInt64)value)<<24) & 0x0000FF0000000000ULL)  | \
  370.                  ((((UInt64)value)<< 8) & 0x000000FF00000000ULL)  | \
  371.                  ((((UInt64)value)>> 8) & 0x00000000FF000000ULL)  | \
  372.                  ((((UInt64)value)>>24) & 0x0000000000FF0000ULL)  | \
  373.                  ((((UInt64)value)>>40) & 0x000000000000FF00ULL)  | \
  374.                  ((((UInt64)value)>>56) & 0x00000000000000FFULL))
  375.     #else
  376.         #define Endian64_Swap(value)                             \
  377.                 (((((UInt64)value)<<56) & 0xFF00000000000000)  | \
  378.                  ((((UInt64)value)<<40) & 0x00FF000000000000)  | \
  379.                  ((((UInt64)value)<<24) & 0x0000FF0000000000)  | \
  380.                  ((((UInt64)value)<< 8) & 0x000000FF00000000)  | \
  381.                  ((((UInt64)value)>> 8) & 0x00000000FF000000)  | \
  382.                  ((((UInt64)value)>>24) & 0x0000000000FF0000)  | \
  383.                  ((((UInt64)value)>>40) & 0x000000000000FF00)  | \
  384.                  ((((UInt64)value)>>56) & 0x00000000000000FF))
  385.     #endif
  386. #else
  387.     /* 
  388.         Note: When using C compilers that don't support "long long",
  389.               Endian64_Swap must be implemented as glue. 
  390.     */
  391.     #ifdef __cplusplus
  392.         inline static UInt64 Endian64_Swap(UInt64 value)
  393.         {
  394.             UInt64 temp;
  395.             temp.lo = Endian32_Swap(value.hi);
  396.             temp.hi = Endian32_Swap(value.lo);
  397.             return temp;
  398.         }
  399.     #else
  400.         extern UInt64 Endian64_Swap(UInt64 value);
  401.     #endif
  402. #endif
  403.  
  404.  
  405.  
  406.  
  407. #if PRAGMA_STRUCT_ALIGN
  408.     #pragma options align=reset
  409. #elif PRAGMA_STRUCT_PACKPUSH
  410.     #pragma pack(pop)
  411. #elif PRAGMA_STRUCT_PACK
  412.     #pragma pack()
  413. #endif
  414.  
  415. #ifdef PRAGMA_IMPORT_OFF
  416. #pragma import off
  417. #elif PRAGMA_IMPORT
  418. #pragma import reset
  419. #endif
  420.  
  421. #ifdef __cplusplus
  422. }
  423. #endif
  424.  
  425. #endif /* __ENDIAN__ */
  426.  
  427.